package ruc.InfluxDB.read;


import okhttp3.Request;
import ruc.Consts.constant;
import ruc.utils.IOUtils;
import ruc.InfluxDB.InfluxDB_Common;
import ruc.utils.Pval;

import java.util.*;

public class InfluxDB_SingleThreadRead {
    String dbName = "";
    String queryURL = "";
    String tbName = "";

    public InfluxDB_SingleThreadRead(String IP, String port, String dbName, String tbName) {
        String baseUrl = String.format("http://%s:%s", IP, port);
        this.queryURL = baseUrl + "/query?db=" + dbName;
        this.dbName = dbName;
        this.tbName = tbName;
    }

    public long convertAndSend(String eSql) {
        String tmp = queryURL + "&q=" + eSql;
        Request request = new Request.Builder()
                .url(tmp)
                .build();

        return InfluxDB_Common.sendRequest(request);
    }

    public void execute(String eSql, int type) {
        long tol = 0;
        HashMap<Integer, List<Long>> m = new HashMap<>();
        for(int i = 0; i < 10000; i++) {
            long st_time = System.nanoTime();
            convertAndSend(eSql);
            long ed_time = System.nanoTime();
            long diff = ed_time - st_time / constant.Ns2Ms;
            int key = Pval.check(diff); // 分桶
            if (m.containsKey(key)) {
                m.get(key).add(diff);
            } else {
                List<Long> l = new ArrayList<>();
                l.add(diff);
                m.put(key, l);
            }
            tol += diff;
        }

        System.out.printf("[TSBench-D] [InfluxDB_query%d] >>>>> avg Response time：%d ms, pc50：%d ms, pc90:%d ms, pc99: %d ms >>>>>> \n", type, tol / 10000, Pval.calPVal(m, 50), Pval.calPVal(m, 90), Pval.calPVal(m, 99));
        IOUtils.WriteData(constant.GetWriteFilePath("InfluxDB", "SingleThreadRead"),
                new String[]{String.valueOf(System.nanoTime()), String.valueOf(type), String.valueOf(tol / 10000), String.valueOf(Pval.calPVal(m, 50)), String.valueOf(Pval.calPVal(m, 90)), String.valueOf(Pval.calPVal(m, 99))});
    }

    public void query1(String serviceName, String hostName, long st, long ed, String diff) {
        String sqlFormat = "select max(cpu_usage) from %s where server='%s' and hostname='%s' and time>='%s' and time<='%s' group by time(%s)";
        String eSql = String.format(sqlFormat, tbName, serviceName, hostName, InfluxDB_Common.DataUtil(st), InfluxDB_Common.DataUtil(ed), diff);
        System.out.println(eSql);
        execute(eSql, 1);
    }

    public void query2(String s1, String s2, String s3, String s4,long st, long ed, String diff) {
        String sqlFormat = "select max(cpu_usage), mean(memory_usage), sum(disk_usage) from %s where (server='%s' or server='%s' or server='%s' or server='%s') and time>= '%s' and time < '%s' group by time(%s)";
        String eSql = String.format(sqlFormat, tbName, s1, s2, s3, s4, InfluxDB_Common.DataUtil(st), InfluxDB_Common.DataUtil(ed), diff);
        System.out.println(eSql);
        execute(eSql, 2);
    }

    public void query3(long st, long ed, String diff) {
        String sqlFormat = "select max(disk_usage) from %s where time>='%s' and time<= '%s' group by time(%s) order by desc";
        String eSql = String.format(sqlFormat, tbName, InfluxDB_Common.DataUtil(st), InfluxDB_Common.DataUtil(ed), diff);
        System.out.println(eSql);
        execute(eSql, 3);
    }

    public void query4(long st, long ed, String diff) {
        String sqlFormat = "select max(disk_usage) from %s where time>='%s' and time<='%s' group by time(%s), hostname";
        String eSql = String.format(sqlFormat, tbName, InfluxDB_Common.DataUtil(st), InfluxDB_Common.DataUtil(ed), diff);
        System.out.println(eSql);
        execute(eSql, 4);
    }

    public static void main(String[] args) {
        InfluxDB_SingleThreadRead db = new InfluxDB_SingleThreadRead(constant.IP, constant.InfluxDB_Port, constant.dbName, constant.Concurrent_tbName);
        String []serverList = constant.server;
        Random ran = new Random();

        db.query1(serverList[ran.nextInt(serverList.length)], "dde8784e-6f96-11ec-bd43-cd6282b60352", 1629877620000L, 1630643400000L, "1d");
        db.query2(serverList[ran.nextInt(serverList.length)], serverList[ran.nextInt(serverList.length)], serverList[ran.nextInt(serverList.length)], serverList[ran.nextInt(serverList.length)], 1629877620000L, 1630643400000L, "1d");
        db.query3(1629877620000L, 1630643400000L, "1d");
        db.query4(1629877620000L, 1630643400000L, "1d");
    }
}
